//
//  SAPFindCarCollectionViewCell.m
//  CarsHome
//
//  Created by dapeng on 15/12/4.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPFindCarCollectionViewCell.h"
#import "Theme-PrefixHeader.pch"
@implementation SAPFindCarCollectionViewCell
- (void)dealloc {
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self button];
        [self tableViews];

        [self addSubview:self.tableView];
        [_tableView release];
        [SAPNetWorkTool getWithUrl:cars parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
            NSDictionary *dic = [result valueForKey:@"result"];
            self.oneModel = [[SAPFindCarOneModel alloc] initWithDataSource:dic];

            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        } fail:^(NSError *error) {
            NSLog(@"%@", error);
        }];
        
        [self changeColor];
    }return self;
}
- (void)changeColor {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
        self.buttonView.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        self.buttonView.tintColor = [UIColor whiteColor];
    }else{
        self.backgroundColor = [UIColor whiteColor];
        self.buttonView.backgroundColor = [UIColor whiteColor];
        
    }

  
}
- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"tgColor"];
    [_tableView registerClass:[SAPFindCarTableViewCell class] forCellReuseIdentifier:@"SAPFindCarTableViewCellIndentifier"];
    [_tableView registerClass:[SAPHotTableViewCell class] forCellReuseIdentifier:@"SAPHotTableViewCellIndentifier"];
    [_tableView registerClass:[SAPRecomCarTableViewCell class] forCellReuseIdentifier:@"SAPRecomCarTableViewCellIndentifier"];
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.tableView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 69);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SAPHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPHotTableViewCellIndentifier"];
        
        cell.delegate = self;
        return cell;
    }
    else if (indexPath.section == 1) {
        SAPRecomCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPRecomCarTableViewCellIndentifier"];
        
        cell.delegate = self;
        return cell;
    }else {
    SAPFindCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPFindCarTableViewCellIndentifier"];
        
    if (self.oneModel) {
        if (indexPath.section < _oneModel.brandlistArray.count) {
            _twoModel = self.oneModel.brandlistArray[indexPath.section - 2];
        }
        if (indexPath.row < _twoModel.listArray.count) {
            SAPFindCarThreeModel *threeModel = _twoModel.listArray[indexPath.row];
            cell.threeModel = threeModel;
            
        }
    }return cell;
}
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.oneModel) {
        if (section == 0) {
            return 1;
        }if (section == 1) {
            return 1;
        }else {
        SAPFindCarTwoModel *two = _oneModel.brandlistArray[section - 2];
        return two.listArray.count;
           
        }
    }return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return self.oneModel.brandlistArray.count + 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (self.oneModel) {
        if (section == 0) {
//            tableView.tableHeaderView = self.buttonView;
        return @"热门品牌";
        }else if (section == 1) {
        return @"主打车";
        }else {
            
            SAPFindCarTwoModel *two = _oneModel.brandlistArray[section - 2];
            return two.letter;
        }
    }return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 180;
    }
    if (indexPath.section == 1) {
        return 100;
    }
    return 64;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section >= 2) {
        if (indexPath.section < _oneModel.brandlistArray.count) {
            _twoModel = self.oneModel.brandlistArray[indexPath.section - 2];
        }
            SAPFindCarThreeModel *threeModel = self.twoModel.listArray[indexPath.row];
            NSString *str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.5/cars/seriesprice-pm1-b%@-t1.json", threeModel.id];
        NSLog(@"ddddddddddddd");
        
            [self.finddelegate passValue:str];
        }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

}
- (void)button {
    self.buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 100)];
    NSArray *nameArray = [NSArray arrayWithObjects:@"我的收藏", @"浏览历史", @"我的电台", nil];
    NSArray *picArray = [NSArray arrayWithObjects:@"wodeshoucang", @"liulanlishi", @"audioIcon2", nil];
    for (int i = 0; i < 3; i++) {
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
        button1.tag = i + 100;
        button1.frame = CGRectMake(i * self.frame.size.width / 3, 0, self.frame.size.width/3, 60);
        [button1 setImage:[[UIImage imageNamed:picArray[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i * self.frame.size.width / 3, 50, self.frame.size.width/3, 30)];
        label.text = nameArray[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:13];

        
        [self.buttonView addSubview:label];
        [self.buttonView addSubview:button1];
    }
    
    _tableView.tableHeaderView = self.buttonView;
    
    [self.tableView reloadData];

}
- (void)buttonDidPress:(UIButton *)sender {
    if (sender.tag == 100) {
        [self.finddelegate pushToCollectVC];
    }else if (sender.tag == 101) {
        [self.finddelegate pushHistoryPage];
    }else {
        [self.finddelegate pushAudioPage];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 30;
    }
    return 15;
}
- (void)passHotCarValue:(NSString *)count {
    _string = count;
    [self.finddelegate passValue:_string];
    
}
- (void)passValue2:(NSString *)count {
    [self.finddelegate passValueWeb:count];
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    return @[@"热", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
}
@end
